<?php

declare(strict_types=1);

namespace app\api\controller\v1;

use app\api\BaseApiController;
use app\admin\model\Token;
use think\facade\Config;
use app\system\model\LogLogin as Log;

class Bind extends BaseApiController
{
    /**
     * 绑定推送Token
     * url: http://域名/api/v1/bind
     * doc: http://lutui.net/doc/app/bind.html
     */
    public function index()
    {
        $token = '';
        $obj = Token::where('admin_id', UID)->where('platform', 'app')->find();
        if ($obj) {
            $pushToken = $this->data['pushToken'];
            $device = $this->data['device'];
            $salt = generateSalt();
            $encryptedPassword = crypt($this->user->password, Config::get('xp.password_salt') . $this->user->salt);
            $auth = $this->user->id . "\t" . $encryptedPassword . "\t" . $this->user->salt . "\t" . $salt . "\t" . $pushToken . "\t" . $device;
            $token = $this->encrypt($auth);
            $obj->token = $token;
            $obj->salt = $salt;
            $obj->push = $pushToken;
            $obj->lang = $this->data['lang'];
            $obj->device = $device;
            $obj->save();
            Log::add($this->user->nickname, '登录成功', 'App', 'Android');
        }
        return json(['error' => '', 'token' => $token]);
    }
}
